﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Configuration;
using Data.UserDb;

namespace Acc.Lib.Providers
{
    public class TicketProvider : ActionProvider
    {
        protected override bool Serv(Orders order, ShopRepository sr)
        {
            var cusr = new UserInfo();
            cusr.GetInfoByUserId(order.UserId);
            var toUsr = new UserInfo();
            toUsr.GetInfoByUserId(order.ToUserId);

            var transType = sr.GetTransType(2);
            var acc = sr.UpdateAccount(cusr.UserId, cusr.UserName, cusr.NickName, transType, order.OrderId.ToString(), -order.Price, String.Format(transType.TransTypeTemplate, order.UserId, -order.Price));
            order.PayTime = DateTime.Now;
            order.PayStatus = 1;
            sr.Save();

            if(acc != null)
            {
                sr.AddBox(order);
                order.SrvTime = DateTime.Now;
                order.SrvStatus = 1;
                sr.Save();
                return true;
            }
            return false;
        }
    }


    public class RoomProvider: ActionProvider
    {

        protected override bool Serv(Orders order, ShopRepository sr)
        {
            var cusrinfo = new UserInfo();
            cusrinfo.GetInfoByUserId(order.UserId); 
            var touser = new UserInfo();
            touser.GetInfoByUserId(order.ToUserId);

            var transType = sr.GetTransType(2);
            var acc = sr.UpdateAccount(cusrinfo.UserId, cusrinfo.UserName, cusrinfo.NickName, transType, order.OrderId.ToString(), -order.Price, String.Format(transType.TransTypeTemplate, order.UserId, -order.Price));
            order.PayTime = DateTime.Now;
            order.PayStatus = 1;
            sr.Save();

            if(acc != null)
            {
                Media.AddRoom(new T_chnl_roominfo
                                  {
                                      Chnlid = 1,
                                      Exp = order.Amount,
                                      Exprate = 0,
                                      Image = "",
                                      Mngerheader = "总裁",
                                      MngerId = touser.UserId,
                                      Mngername = touser.UserName,
                                      Roomdesc = "",
                                      Roomfee = 1,
                                      Roomname = touser.UserName + "的房间",
                                      Usercount = 0,
                                      Roomflag = 1,
                                      Roomipaddr = "",
                                      Vsb = 0
                                     
                                  });
                order.SrvTime = DateTime.Now;
                order.SrvStatus = 1;
                sr.Save();

                return true;
            }
            
            return false;
        }
    }

    public class VipProvider:ActionProvider
    {

        protected override bool Serv(Orders order, ShopRepository sr)
        {
            var cusrinfo = new UserInfo();
            cusrinfo.GetInfoByUserId(order.UserId);
            var touser = new UserInfo();
            touser.GetInfoByUserId(order.ToUserId);

            var transType = sr.GetTransType(2);
            var acc = sr.UpdateAccount(cusrinfo.UserId, cusrinfo.UserName, cusrinfo.NickName, transType, order.OrderId.ToString(), -order.Price, String.Format(transType.TransTypeTemplate, order.UserId, -order.Price));
           
            order.PayTime = DateTime.Now;
            order.PayStatus = 1;
            sr.Save();

            if(acc != null)
            {
                touser.SetVip(touser.UserId);

                order.SrvTime = DateTime.Now;
                order.SrvStatus = 1;
                sr.Save();
                return true;
            }
            
            return true;
        }
    }

    public class UpdateAccountProvider : ActionProvider
    {
        protected override bool Serv(Orders order, ShopRepository sr)
        {
            
            var usrInfo = new UserInfo();
            usrInfo.GetInfoByUserId(order.ToUserId);

            try
            {
                
                var transType = sr.GetTransType(1);

                var acc = sr.UpdateAccount(usrInfo.UserId, usrInfo.UserName, usrInfo.NickName, transType, order.OrderId.ToString(), order.Amount, String.Format(transType.TransTypeTemplate, order.UserId, order.Amount));
                order.SrvTime = DateTime.Now;
                order.SrvStatus = 1;
                order.PayTime = DateTime.Now;
                order.PayStatus = 1;

                sr.Save();
                return acc != null ;
            }
            catch(Exception ex)
            {
                return false;
            }
        }
    }
}
